//
//  LKBoTongFeedbackWebViewController+Landscape.m
//  iOS-BTong-ApplicationCenter
//
//  Created by maqz on 2018/4/3.
//  Copyright © 2018年 毛毛驴. All rights reserved.
//

#import "LKBoTongFeedbackWebViewController+Landscape.h"

@implementation LKBoTongFeedbackWebViewController (Landscape)

NSString *BotongApplicationCenterLandscapeJSHandler = @"BotongApplicationCenterLandscapeJSHandler";
static NSString const*scriptIsLandscape = @"isLandscape";

- (void)scriptLandscapeMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:BotongApplicationCenterLandscapeJSHandler]) {
        NSDictionary *params = message.body[scriptParams];
        if (!params || ![params isKindOfClass:[NSDictionary class]]) {
            return;
        }
        BOOL isLandscape = [params[scriptIsLandscape] boolValue];
        
        id delegate = [UIApplication sharedApplication].delegate;
        SEL selector = NSSelectorFromString(@"setAllowAutoRotate:");
        if ([delegate respondsToSelector:selector]) {
            NSMethodSignature *methodSignature = [delegate methodSignatureForSelector:selector];
            NSInvocation *invcation = [NSInvocation invocationWithMethodSignature:methodSignature];
            [invcation setTarget:delegate];
            [invcation setSelector:selector];
            [invcation setArgument:&isLandscape atIndex:2];
            [invcation invoke];
        }
        
        
        
        [self setDeviceInterfaceOrientation:isLandscape ? UIDeviceOrientationLandscapeLeft : UIDeviceOrientationPortrait];
    }
}

- (void)setDeviceInterfaceOrientation:(UIDeviceOrientation)orientation {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orientation] forKey:@"orientation"];
    }
}


@end
